<!DOCTYPE html>
<html>
<head>
    <title>Thermodynamics Piston Diagram</title>
</head>
<body>
<canvas id="physicsCanvas" width="600" height="400"></canvas>
<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    // Style settings
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    ctx.lineWidth = 2.5; // Slightly thicker lines to match the drawing style
    ctx.lineCap = 'round'; // Smoother line ends

    // Container dimensions
    const containerX = 100;
    const containerY = 150;
    const containerWidth = 400;
    const containerHeight = 80;

    // Draw container rectangle
    ctx.strokeRect(containerX, containerY, containerWidth, containerHeight);

    // Piston dimensions and position
    const pistonWidth = 16;
    const pistonX = containerX + containerWidth / 2 - pistonWidth / 2;
    const pistonY = containerY;
    const pistonHeight = containerHeight;

    // Draw piston outline
    ctx.strokeRect(pistonX, pistonY, pistonWidth, pistonHeight);

    // Draw piston hatching (diagonal lines)
    ctx.save();
    ctx.beginPath();
    ctx.rect(pistonX, pistonY, pistonWidth, pistonHeight);
    ctx.clip(); // Restrict drawing to the piston's rectangle
    ctx.lineWidth = 2;
    const spacing = 5;
    // Draw diagonal lines from top-left towards bottom-right
    for (let i = -pistonHeight; i < pistonWidth + spacing; i += spacing) {
        ctx.moveTo(pistonX + i, pistonY);
        ctx.lineTo(pistonX + i + pistonHeight, pistonY + pistonHeight);
    }
    ctx.stroke();
    ctx.restore(); // Restore context to pre-clipping state

    // Set font style for labels, trying to mimic the original's handwritten feel
    const labelFont = '28px "Comic Sans MS", cursive, sans-serif';
    ctx.font = labelFont;
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';

    // Draw text labels inside the container
    ctx.fillText('neon', containerX + containerWidth / 4, containerY + containerHeight / 2);
    ctx.fillText('argon', containerX + 3 * containerWidth / 4, containerY + containerHeight / 2);

    // Draw "piston" label
    const pistonLabelX = pistonX + 55;
    const pistonLabelY = containerY - 40;
    ctx.fillText('piston', pistonLabelX, pistonLabelY);

    // Draw leader line pointing to the piston
    ctx.lineWidth = 2;
    ctx.beginPath();
    // Start point near the text
    const startX = pistonLabelX - 25;
    const startY = pistonLabelY + 12;
    // End point on the top edge of the piston
    const endX = pistonX + pistonWidth;
    const endY = containerY;
    ctx.moveTo(startX, startY);
    // Use a quadratic curve to create the arc
    ctx.quadraticCurveTo(startX, startY + 15, endX, endY);
    ctx.stroke();
    // Draw a filled circle at the connection point to mimic the original
    ctx.beginPath();
    ctx.arc(endX, endY, 3, 0, 2 * Math.PI);
    ctx.fill();

    // Draw "Fig. 1.31." label below the container
    ctx.font = '24px "Times New Roman"';
    ctx.fillText('Fig. 1.31.', containerX + containerWidth / 2, containerY + containerHeight + 45);

</script>
</body>
</html>